package com.inews.business.web.excel;

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.inews.base.utils.Servlets;
import com.inews.base.vo.ExtData;
import com.inews.business.service.DegreeStudyService;
import com.inews.business.service.RetireStaffService;
import com.inews.business.vo.DegreeStudyVo;
import com.inews.business.vo.RetireStaffVo;

@Component
public class DegreeStudyExcelView extends AbstractExcelView {
	
	private static Logger logger = Logger.getLogger(DegreeStudyExcelView.class);

	@Autowired
	private DegreeStudyService service;

	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String jsonFilter = request.getParameter("jsonFilter");
		logger.info("接收到的查询条件json：" + jsonFilter);
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(jsonFilter, "search_");
		ExtData<DegreeStudyVo> extData = service.exportExcel(searchParams);
		List<DegreeStudyVo> voList = extData.getRoot();

		String fileName = URLEncoder.encode("学历学位进修信息", "UTF-8");
		// 设置response方式,使执行此controller时候自动出现下载页面,而非直接使用excel打开
		response.setContentType("APPLICATION/OCTET-STREAM");
		response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");

		// 产生Excel表头
		HSSFSheet sheet = workbook.createSheet("学历学位进修信息");
		HSSFRow header = sheet.createRow(0); // 第0行
		// 产生标题列
		header.createCell(0).setCellValue("编号");
		header.createCell(1).setCellValue("部门");
		header.createCell(2).setCellValue("工号");
		header.createCell(3).setCellValue("教师姓名");
		header.createCell(4).setCellValue("年龄");
		header.createCell(5).setCellValue("进修内容");
		header.createCell(6).setCellValue("进修学校");
		header.createCell(7).setCellValue("所修专业");
		header.createCell(8).setCellValue("开始时间");
		header.createCell(9).setCellValue("结束时间");
		header.createCell(10).setCellValue("备注");

		// 填充数据
		int rowNum = 1;
		for (DegreeStudyVo vo : voList) {
			HSSFRow row = sheet.createRow(rowNum++);
			row.createCell(0).setCellValue(vo.getId());
			row.createCell(1).setCellValue(vo.getDepartment());
			row.createCell(2).setCellValue(vo.getStaffId());
			row.createCell(3).setCellValue(vo.getStaffName());
			row.createCell(4).setCellValue(vo.getAge());
			row.createCell(5).setCellValue(vo.getContent());
			row.createCell(6).setCellValue(vo.getSchool());
			row.createCell(7).setCellValue(vo.getSubject());
			row.createCell(8).setCellValue(vo.getStartTimeFormat());
			row.createCell(9).setCellValue(vo.getEndTimeFormat());
			row.createCell(10).setCellValue(vo.getRemark());
		}

	}

}
